<pre class="metadata">
Title: Magnetometer
Level: none
Status: ED
ED: https://w3c.github.io/magnetometer/
Shortname: magnetometer
TR: https://www.w3.org/TR/magnetometer/
Previous Version: https://www.w3.org/TR/2018/CR-magnetometer-20180320/
Editor: Anssi Kostiainen 41974, Intel Corporation, https://intel.com/
Editor: Rijubrata Bhaumik 80407, Intel Corporation, https://intel.com/
Group: dap
Abstract:
  This specification defines a concrete sensor interface to measure magnetic
  field in the X, Y and Z axis.
Version History: https://github.com/w3c/magnetometer/commits/master/index.bs
!Bug Reports: <a href="https://www.github.com/w3c/magnetometer/issues/new">via the w3c/magnetometer repository on GitHub</a>
Indent: 2
Repository: w3c/magnetometer
Markup Shorthands: markdown on
Inline Github Issues: true
!Test Suite: <a href="https://github.com/web-platform-tests/wpt/tree/master/magnetometer">web-platform-tests on GitHub</a>
Boilerplate: omit issues-index, omit conformance
</pre>
<pre class="anchors">
urlPrefix: https://w3c.github.io/sensors/; spec: GENERIC-SENSOR
  type: dfn
    text: high-level
    text: sensor
    text: latest reading
    text: default sensor
    text: construct a sensor object; url: construct-sensor-object
    text: initialize a sensor object; url: initialize-a-sensor-object
    text: sensor type
    text: mitigation strategies; url: mitigation-strategies
    text: local coordinate system
    text: sensor readings; url: sensor-reading
    text: check sensor policy-controlled features; url: check-sensor-policy-controlled-features
    text: keystroke monitoring; url: keystroke-monitoring
    text: sensor permission name; url: sensor-permission-names
    text: supported sensor options
    text: automation
    text: mock sensor type
    text: mock sensor reading values
urlPrefix: https://w3c.github.io/accelerometer/; spec: ACCELEROMETER
  type: dfn
    text: device coordinate system
    text: screen coordinate system
urlPrefix: https://w3c.github.io/motion-sensors/; spec: MOTIONSENSORS
  type: dfn
    text: Absolute Orientation Sensor; url: absolute-orientation
</pre>


<pre class=biblio>
{
   "MAGITACT": {
        "authors": [
            "Hamed Ketabdar, Kamer Ali Yüksel, Mehran Roshandel"
        ],
        "id": "MAGITACT",
        "href": "http://dl.acm.org/citation.cfm?id=1720048",
        "title": "Magitact",
        "status": "Informational",
        "publisher": "ACM"
    },
    "VRBUTTON": {
        "authors": [
            "Boris Smus"
        ],
        "id": "VRBUTTON",
        "href": "https://bugs.chromium.org/p/chromium/issues/detail?id=445926",
        "title": "Magnetic input for Google cardboard",
        "status": "Informational",
        "publisher": "Unknown"
    },
    "MAGINDOORPOS": {
        "authors": [
            "Janne Haverinen, Anssi Kemppainen"
        ],
        "id": "MAGINDOORPOS",
        "href": "https://doi.org/10.1016%2Fj.robot.2009.07.018",
        "title": "Global indoor self-localization based on the ambient magnetic field",
        "status": "Informational",
        "publisher": "Robotics and Autonomous Systems"
    }
}
</pre>
<pre class="link-defaults">
spec:infra;
  type:dfn;
    text:list
spec:generic-sensor-1;
  type:enum-value;
    text:"activated"
spec: webidl;
  type:dfn;
    text:identifier
</pre>

Introduction {#intro}
============

Magnetometer extends the Generic Sensor API [[GENERIC-SENSOR]]
to provide information about the <a>magnetic field</a>
as detected by the device's primary magnetometer sensor.
The magnetometer sensor measures the <a>magnetic field</a> for all three
physical axes (x, y, z) in μT (micro Tesla).

This specification defines two new interfaces:
-   {{Magnetometer}} that reports <a>calibrated magnetic field</a> values, and
-   {{UncalibratedMagnetometer}} that reports <a>uncalibrated magnetic field</a> values.

The <dfn>magnetic field</dfn> is a field that exerts magnetic force on magnetometer sensor
due to the magnetic effect generated by electric currents, magnetic materials or Earth's magnetic
force that is attributed to the combined effects of the planetary rotation and the movement of molten
iron in the Earth's core.

<dfn>Hard iron distortion</dfn> is created by objects
that produce a <a>magnetic field</a>, such as magnetized iron.

<dfn>Soft iron distortion</dfn> stretches or distorts the
<a>magnetic field</a> and is caused by metals such as
nickel and iron.

The <dfn>calibrated magnetic field</dfn> is a <a>magnetic field</a> with <a>hard iron distortion</a> and <a>soft iron distortion</a>
correction applied.

The <dfn>uncalibrated magnetic field</dfn> is the
<a>magnetic field</a> without <a>hard iron distortion</a>
correction and with <a>soft iron distortion</a> correction applied,
and as such reports changes in the <a>magnetic field</a>
caused by magnetized objects moving near the magnetometer.

Examples {#examples}
========

<div class="example">
    <pre highlight="js">
    let sensor = new Magnetometer();
    sensor.start();

    sensor.onreading = () => {
        console.log("Magnetic field along the X-axis " + sensor.x);
        console.log("Magnetic field along the Y-axis " + sensor.y);
        console.log("Magnetic field along the Z-axis " + sensor.z);
    };

    sensor.onerror = event => console.log(event.error.name, event.error.message);
    </pre>
</div>

Security and Privacy Considerations {#security-and-privacy}
===================================

Magnetometer provides information about magnetic field, and in theory, can expose location of a
user. For example, attack vector could be pre-magnetized surface in a particular location, or
mapping between location and constant magnetic field disturbances caused by the building. Due
to non-uniform strength of the Earth’s magnetic field, another attack vector could be exposure or
validation of the user's location. For example, if the end user is connected through VPN, magnetic
field associated with geo IP information can be compared with magnetometer readings at real
location, therefore, tell whether user is using VPN or not.

Uncalibrated magnetometer readings could be affected by magnetized objects nearby, such as jewelry,
thereby exposing information that might be used for [=keystroke monitoring=].

To mitigate these specific threats, user agents should
use one or both of the following mitigation strategies:
  - <a>limit maximum sampling frequency</a>
  - <a>reduce accuracy</a> of sensor readings

These mitigation strategies complement the [=mitigation strategies|generic mitigations=] defined in
the Generic Sensor API [[!GENERIC-SENSOR]].

Model {#model}
=====

The <dfn id="magnetometer-sensor-type">Magnetometer</dfn> <a>sensor type</a> has two associated {{Sensor}} subclasses, {{Magnetometer}} and {{UncalibratedMagnetometer}}.

The {{Magnetometer}} and {{UncalibratedMagnetometer}} have an associated [=sensor permission name=] which is <a for="PermissionName" enum-value>"magnetometer"</a>.

The [=latest reading=] for a {{Sensor}} of <a>Magnetometer</a> <a>sensor type</a> includes three [=map/entries=]
whose [=map/keys=] are "x", "y", "z" and whose [=map/values=] contain <a>magnetic field</a>
about the corresponding axes. Values can contain also device's [=uncalibrated magnetic field=] and [=hard iron distortion=]
depending on which object was instantiated.

For uncalibrated magnetometer, the [=latest reading=] includes three [=map/entries=] whose [=map/keys=] are "x", "y", "z" and whose [=map/values=] contain <a>uncalibrated magnetic field</a> around the 3 different axes,
and three additional [=map/entries=] whose [=map/keys=] are "xBias", "yBias", "zBias" and whose [=map/values=] contain the <a>hard iron distortion</a> correction around the 3 different axes.

The sign of the <a>magnetic field</a> values must be according to the
right-hand convention in a [=local coordinate system=] (see figure below).


<img src="images/magnetometer_coordinate_system.svg" onerror="this.src='images/magnetometer_coordinate_system.png'" style="display: block;margin: auto;" alt="Magnetometer coordinate system.">

Reference Frame {#reference-frame}
----------------

The [=local coordinate system=] represents the reference frame for the
{{Magnetometer}} and the {{UncalibratedMagnetometer}} [=sensor readings|readings=].
It can be either the [=device coordinate system=] or the [=screen coordinate system=].

API {#api}
===

The Magnetometer Interface {#magnetometer-interface}
--------------------------------

<pre class="idl">
  [SecureContext,
    Exposed=Window]
  interface Magnetometer : Sensor {
    constructor(optional MagnetometerSensorOptions sensorOptions = {});
    readonly attribute double? x;
    readonly attribute double? y;
    readonly attribute double? z;
  };

  enum MagnetometerLocalCoordinateSystem { "device", "screen" };

  dictionary MagnetometerSensorOptions : SensorOptions {
    MagnetometerLocalCoordinateSystem referenceFrame = "device";
  };
</pre>

To construct a {{Magnetometer}} object the user agent must invoke the
[=construct a magnetometer object=] abstract operation for the {{Magnetometer}}
interface.

[=Supported sensor options=] for {{Magnetometer}} are "frequency" and "referenceFrame".

### Magnetometer.x ### {#magnetometer-x}


The {{Magnetometer/x!!attribute}} attribute of the {{Magnetometer}}
interface represents the <a>magnetic field</a> around X-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "x" as arguments.


### Magnetometer.y ### {#magnetometer-y}


The {{Magnetometer/y!!attribute}} attribute of the {{Magnetometer}}
interface represents the <a>magnetic field</a> around Y-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "y" as arguments.


### Magnetometer.z ### {#magnetometer-z}


The {{Magnetometer/z!!attribute}} attribute of the {{Magnetometer}}
interface represents the <a>magnetic field</a> around Z-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "z" as arguments.


The UncalibratedMagnetometer Interface {#uncalibrated-magnetometer-interface}
--------------------------------

<pre class="idl">
  [SecureContext,
    Exposed=Window]
  interface UncalibratedMagnetometer : Sensor {
    constructor(optional MagnetometerSensorOptions sensorOptions = {});
    readonly attribute double? x;
    readonly attribute double? y;
    readonly attribute double? z;
    readonly attribute double? xBias;
    readonly attribute double? yBias;
    readonly attribute double? zBias;
  };
</pre>


To construct an {{UncalibratedMagnetometer}} object the user agent must invoke the
[=construct a magnetometer object=] abstract operation for the {{UncalibratedMagnetometer}}
interface.

[=Supported sensor options=] for {{UncalibratedMagnetometer}} are "frequency" and "referenceFrame".

### UncalibratedMagnetometer.x ### {#uncalibrated-magnetometer-x}


The {{UncalibratedMagnetometer/x!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>uncalibrated magnetic field</a> around X-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "x" as arguments.


### UncalibratedMagnetometer.y ### {#uncalibrated-magnetometer-y}


The {{UncalibratedMagnetometer/y!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>uncalibrated magnetic field</a> around Y-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "y" as arguments.


### UncalibratedMagnetometer.z ### {#uncalibrated-magnetometer-z}


The {{UncalibratedMagnetometer/z!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>uncalibrated magnetic field</a> around Z-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "z" as arguments.


### UncalibratedMagnetometer.xBias ### {#uncalibrated-magnetometer-xBias}


The {{UncalibratedMagnetometer/xBias!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>hard iron distortion</a> correction around X-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "xBias" as arguments.

### UncalibratedMagnetometer.yBias ### {#uncalibrated-magnetometer-yBias}


The {{UncalibratedMagnetometer/yBias!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>hard iron distortion</a> correction around Y-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "yBias" as arguments.

### UncalibratedMagnetometer.zBias ### {#uncalibrated-magnetometer-zBias}


The {{UncalibratedMagnetometer/zBias!!attribute}} attribute of the {{UncalibratedMagnetometer}}
interface represents the <a>hard iron distortion</a> correction around Z-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with `this` and "zBias" as arguments.


Abstract Operations {#abstract-opertaions}
==============

<h3 dfn>Construct a magnetometer object</h3>

<div algorithm="construct a magnetometer object">

    : input
    :: |magnetometer_interface|, a {{Magnetometer}} [=interface=] [=identifier=] or
       an {{UncalibratedMagnetometer}} [=interface=] [=identifier=].
    :: |options|, a {{MagnetometerSensorOptions}} object.
    : output
    :: A {{Magnetometer}} or {{UncalibratedMagnetometer}} object.

    1.  Let |allowed| be the result of invoking [=check sensor policy-controlled features=]
        with {{Magnetometer}}.
    1.  If |allowed| is false, then:
        1.  [=Throw=] a {{SecurityError}} {{DOMException}}.
    1.  Let |magnetometer| be a new instance of the [=interface=] identified by |magnetometer_interface|.
    1.  Invoke [=initialize a sensor object=] with |magnetometer| and |options|.
    1.  If |options|.{{referenceFrame!!dict-member}} is "screen", then:
        1.  Define [=local coordinate system=] for |magnetometer|
            as the [=screen coordinate system=].
    1.  Otherwise, define [=local coordinate system=] for |magnetometer|
        as the [=device coordinate system=].
    1.  Return |magnetometer|.
</div>

Automation {#automation}
==========
This section extends the [=automation=] section defined in the Generic Sensor API [[GENERIC-SENSOR]]
to provide mocking information about the [=magnetic field=] in the X, Y and Z axis
for the purposes of testing a user agent's implementation of {{Magnetometer}} and {{UncalibratedMagnetometer}} APIs.

<h3 id="mock-magnetometer-type">Mock Sensor Type</h3>

The {{Magnetometer}} class has an associated [=mock sensor type=] which is
<a for="MockSensorType" enum-value>"magnetometer"</a>, its [=mock sensor reading values=]
dictionary is defined as follows:

<pre class="idl">
  dictionary MagnetometerReadingValues {
    required double? x;
    required double? y;
    required double? z;
  };
</pre>

The {{UncalibratedMagnetometer}} class has an associated [=mock sensor type=] which is
<a for="MockSensorType" enum-value>"uncalibrated-magnetometer"</a>, its [=mock sensor reading values=]
dictionary is defined as follows:

<pre class="idl">
  dictionary UncalibratedMagnetometerReadingValues {
    required double? x;
    required double? y;
    required double? z;
    required double? xBias;
    required double? yBias;
    required double? zBias;
  };
</pre>

Limitations of Magnetometer Sensors {#limitations-magnetometer}
==============

<em>This section is non-normative</em>.

The direction and magnitude of the Earth’s field changes with location, latitude in particular. For example,
the magnitude is lowest near the equator and highest near the poles.
Some hard-iron interference, meaning presence of permanent magnets (e.g. magnets in the speaker of a phone) in the vicinity of the sensor
also affects the accuracy of the reading.
Presence of electronic items, laptops, batteries, etc also contribute to the soft iron interference.
Flight Mode option in mobile phones might help in decreasing the electro magnetic interference.

In addition to the above spatial variations of the <a>magnetic field</a>, time based variations,
like solar winds or magnetic storms, also distort the magnetosphere or external magnetic field of the Earth.

Use Cases and Requirements {#usecases-and-requirements}
===================

<em>This section is non-normative</em>.

Magnetometers can be used for a variety of use-cases, for example:

- Sensor fusion. A common use-case for magnetometers is sensor fusion in order to generate an <a>Absolute Orientation Sensor</a> [[MOTION-SENSORS]] which is stationary to the Earth plane, or a compass, which is basically the former with corrections to the declination depending on geolocation position, such that it points to the true north. Calculating compass heading as detailed in [[#compass]].
- Virtual Reality and Augmented Reality. Magnetometer can be used to implement input using magnetic button for VR enclosures [[VRBUTTON]]. Head-mount tracking systems for VR and AR can use magnetometer data to help in calibration of gyroscope readings and align yaw readings with the magnetic north.
- Gesture recognition. Various interactions like writing, signing and playing an instrument can also be enabled using a magnet like a rod, pen or a ring [[MAGITACT]].
  The user makes coarse gestures in the 3D space around the device using the magnet. Movement of the magnet affects the magnetic field sensed by the compass sensor integrated in the device.
  The temporal pattern of the gesture can be used as a basis for sending different interaction commands to the mobile device. Zooming, turning pages, accepting/rejecting calls, clicking items
  are some of the use cases.
- Indoor navigation. Navigation systems can use magnetometer data on mobile devices [[MAGINDOORPOS]] to detect the magnetic field inside a building. With sufficient local variability, the anomalies can be utilized in self-localization. Use cases for indoor navigation include, for example, proximity advertising, way finding in malls or airports, and geofencing.
- Metal detection. Magnetometers can be used by utility applications to detect the presence of metal nearby, e.g. finding inclusions hidden within objects.

<div class="note">
Requirements with respect to data coarseness and sampling frequency can vary depending on the use case at hand. For example, metal detection or input using magnetic button can likely be implemented with coarser data and using lower sampling frequency compared to gesture recognition, indoor navigation, or VR and AR use cases. In sensor fusion use cases, the sampling frequency that yeilds optimal results is dependent on e.g. sensor fusion algorithm and characteristics of other motion sensors involved.
</div>

Compass Heading Using Magnetometers {#compass}
======================

<em>This section is non-normative</em>.

Compasses, instruments that align themselves with the magnetic poles of the Earth, have been used in navigation for centuries.
The Earth’s rotational axis defines the geographic north and south poles that we use for
map references. It turns out that there is a discrepancy of around 11.5 degrees (around 1000 miles) between the geographic poles and the
magnetic poles. <a>Declination angle</a> is applied to the magnetic direction to correct for this situation.

If the device is always level to the Earth’s surface, compass heading can be determined by using just the
{{Magnetometer/x!!attribute}} and {{Magnetometer/y!!attribute}} component of the Earth’s magnetic field, that
is, the directions planar with the Earth’s surface.
To determine geographic north (or true north) heading, add the appropriate <a>declination angle</a>.

<dfn>Magnetic declination</dfn> or <dfn>declination angle</dfn> is the angle on the horizontal plane between magnetic north and the true north and depends on the position on the Earth's surface, and changes over time.
By convention, declination is positive when magnetic north is east of true north, and negative when it is to the west. You can get real time value for <a>magnetic declination</a> e.g. using the <a href="https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml">Magnetic declination calculator</a>
provided by the National Oceanic and Atmospheric Administration (NOAA).

The magnetic north is calculated as follows:

<div class="example">
  <pre highlight="js">
    let sensor = new Magnetometer();
    sensor.start();
    let heading = Math.atan2(sensor.y, sensor.x) * (180 / Math.PI);
    console.log('Heading in degrees: ' + heading);
  </pre>
</div>

The geographic north at a given latitude and longitude can be calculated as follows:

<div class="example">
  <pre highlight="js">
    // Get the latitude and longitude, omitted for brevity here.
    let latitude = 0, longitude = 0;

    // Get the magnetic declination at the given latitude and longitude.
    fetch('https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination' +
          '?lat1=' + latitude + '&lon1=' + longitude + '&resultFormat=csv')
      .then(response => response.text()).then(text => {
        let declination = parseFloat(text.replace(/^#.*$/gm, '').trim().split(',')[4]);

        // Compensate for the magnetic declination to get the geographic north.
        console.log('True heading in degrees: ' + (heading + declination));
    });
    </pre>
</div>


Note: If the device is not level to the Earth’s surface, a developer needs
to apply various tilt compensation techniques for which she needs a 3-axis
accelerometer. Data from the orientation sensor, which is a fusion of the
accelerometer and magnetometer sensors, is required to implement this
particular use case.


Acknowledgements {#acknowledgements}
================

Tobie Langel for the work on Generic Sensor API.

Conformance {#conformance}
===========

Conformance requirements are expressed with a combination of
descriptive assertions and RFC 2119 terminology. The key words "MUST",
"MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT",
"RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this
document are to be interpreted as described in RFC 2119.
However, for readability, these words do not appear in all uppercase
letters in this specification.

All of the text of this specification is normative except sections
explicitly marked as non-normative, examples, and notes. [[!RFC2119]]

A <dfn>conformant user agent</dfn> must implement all the requirements
listed in this specification that are applicable to user agents.

The IDL fragments in this specification must be interpreted as required for
conforming IDL fragments, as described in the Web IDL specification. [[!WEBIDL]]
